const sysDao = require('../dao/sys');

class SysService {

    queryAllSys(req, res){
        const requestMap = req.body;
        sysDao.all(requestMap).then(result => {
            res.json({
                list: result,
                errCode: "000000",
            });
        },err => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        });
    }

    addSys(req, res){
        const sysMap = req.body;
        sysDao.insert(sysMap).then(result => {
            res.json({
                id: result,
                errCode: "000000",
            });
        }, err => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        })
    }

    updateSys(req, res){
        const id = req.param("id");
        if(id === null || id===undefined){
            res.json({
                errMsg: "id不能为空",
                errCode: "id is required",
            });
            return;
        }
        sysDao.update(req.body).then(result => {
            res.json({
                errCode: "000000"
            });
        },(err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        });
    }

    deleteSys(req, res){
        const id = req.param("id");
        if(id === null || id===undefined){
            res.json({
                errMsg: "id不能为空",
                errCode: "id is required",
            });
            return;
        }
        sysDao.delete(id).then(result => {
            res.json({
                errCode: "000000"
            });
        },(err) => {
            res.json({
                errMsg: err.errno,
                errCode: err.code,
            });
        })
    }

}

const sysService = new SysService();
module.exports = sysService;